package hander

import (
	"github.com/gin-gonic/gin"
	"golang-websocket-chatroom/api/request"
	"golang-websocket-chatroom/constss"
	"golang-websocket-chatroom/internal/dao"
	"golang-websocket-chatroom/internal/model"
	"net/http"
)

func AddRoom(c *gin.Context) {
	var req request.AddRoomReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	room := model.Room{
		UserId: c.GetUint("userId"),
		Name:   req.Name,
	}
	dao.Create(&room)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "成功",
		"code": 200,
		"data": "AddRoom",
	})
}

func AddRoomUser(c *gin.Context) {
	var req request.AddRoomUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	var ru model.Room
	dao.GetOneById(req.RoomId, &ru)
	if ru.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "聊天室不存在",
			"code": 500,
			"data": nil,
		})
		return
	}
	roomUser := model.RoomUser{
		RoomId: req.RoomId,
		UserId: c.GetUint("userId"),
		CType:  constss.CTYPE,
	}
	dao.Create(&roomUser)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "成功",
		"code": 200,
		"data": "AddRoomUser",
	})
}

func LoginRoom(c *gin.Context) {
	var req request.LoginRoomReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	var r model.Room
	dao.GetOneById(req.RoomId, &r)
	if r.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "没有此聊天室",
			"code": 500,
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "成功",
		"code": 200,
		"data": r,
	})
}

func AddChat(c *gin.Context) {
	var req []request.AddChatReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	for _, chatReq := range req {
		friend1 := model.ChatHistory{
			UserOne: chatReq.UserTwo,
			UserTwo: c.GetUint("userId"),
			RoomId:  chatReq.RoomId,
			Content: chatReq.Content,
		}
		friend2 := model.ChatHistory{
			UserOne: c.GetUint("userId"),
			UserTwo: chatReq.UserTwo,
			RoomId:  chatReq.RoomId,
			Content: chatReq.Content,
		}
		dao.Create(&friend1)
		dao.Create(&friend2)
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "成功",
		"code": 200,
		"data": "AddChat",
	})
}

func SelectChat(c *gin.Context) {
	var req request.SelectChatReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	var r model.Room
	dao.GetOneById(req.RoomId, &r)
	if r.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "没有此聊天室",
			"code": 500,
			"data": nil,
		})
		return
	}
	var hi model.ChatHistory
	dao.GetOneById(req.RoomId, &hi)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "成功",
		"code": 200,
		"data": hi,
	})
}

func SelectHistory(c *gin.Context) {
	var req request.SelectHistoryReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	var r model.ChatHistory
	dao.GetOneByContent(req.Content, &r)
	if r.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "该记录不存在",
			"code": 500,
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "成功",
		"code": 200,
		"data": r,
	})
}
